home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / 3dprograms / t3dlib / source / writedxf.c < prev    next >
C/C++ Source or Header  |  1995-02-13  |  3KB  |  119 lines

  1. /* writedxf.c - dump the internal database to a DXF file
  2.  *            - written by Glenn M. Lewis - 07/07/92
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: writedxf.c,v 1.5 1993/01/30 12:55:49 glewis Exp $";
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "t3dlib.h"
  10. #ifdef __STDC__
  11. #include <stdlib.h>
  12. #include <strings.h>
  13. #include "writedxf_protos.h"
  14. #endif
  15.  
  16. static void process_DESC();
  17. static void process_INFO();
  18. static FILE *out;
  19.  
  20. /* Here are a few necessary utilities */
  21.  
  22. static void send_XYZ(i, f)            /* Print a common string */
  23. int i;
  24. XYZ_st *f;
  25. {
  26.     fprintf(out, " %d\n%.12g\n", 10+i, f->x);
  27.     fprintf(out, " %d\n%.12g\n", 20+i, f->y);
  28.     fprintf(out, " %d\n%.12g\n", 30+i, f->z);
  29. }
  30.  
  31. static void send_RGB(rgb)            /* Print a common string */
  32. RGB_st *rgb;
  33. {
  34.     fprintf(out, "%.12g\t%.12g\t%.12g\n",
  35.         ((double)rgb->r)/255.0,
  36.         ((double)rgb->g)/255.0,
  37.         ((double)rgb->b)/255.0);
  38. }
  39.  
  40. /********************/
  41. /* The MAIN section */
  42. /********************/
  43.  
  44. int write_DXF(world, file)
  45. WORLD *world;
  46. FILE *file;
  47. {
  48.     register OBJECT *o;
  49.  
  50.     if (!(out = file) || !world) return(0);
  51.  
  52.     /* Write header to DXF file: */
  53.     fprintf(out, "  0\nSECTION\n  2\nENTITIES\n");
  54.  
  55.     if (world->info) process_INFO(world->info);
  56.  
  57.     for (o=world->object; o; o=o->next)
  58.         if (!o->extr) process_DESC(o);
  59.  
  60.     fprintf(out, "  0\nENDSEC\n  0\nEOF\n");
  61.  
  62.     return(1);
  63. }
  64.  
  65. static void process_DESC(object)
  66. OBJECT *object;
  67. {
  68.     register int i;
  69.     register OBJECT *obj;
  70.     register DESC *desc = object->desc;
  71.     register int p1, p2, p3;
  72.  
  73.     /* Process children first */
  74.     for (obj=object->child; obj; obj=obj->next) {
  75.         if (!obj->extr) process_DESC(obj);
  76.     }
  77.  
  78.     if (!desc->pcount || !desc->fcount) return;
  79.  
  80. /*    fprintf(out, "\n# Start of new object\n");    */
  81.  
  82.     for (i=0; i<desc->fcount; i++) {
  83.     /* First check to make sure that this triangle is real */
  84.         p1 = desc->edge[(desc->face[i*3])<<1];
  85.         p2 = desc->edge[((desc->face[i*3])<<1)+1];
  86.         if (p1 == p2) continue;    /* How did *this* happen? */
  87.         p3 = desc->edge[(desc->face[i*3+2])<<1];
  88.         if (p1 == p3 || p2 == p3)
  89.             p3 = desc->edge[((desc->face[i*3+2])<<1)+1];
  90.         if (p1 == p3 || p2 == p3) continue; /* How did *this* happen? */
  91.         /* Now check the actual points for equality */
  92.         if (bcmp(&desc->pnts[p1], &desc->pnts[p2], sizeof(XYZ_st))==0 ||
  93.             bcmp(&desc->pnts[p1], &desc->pnts[p3], sizeof(XYZ_st))==0 ||
  94.             bcmp(&desc->pnts[p2], &desc->pnts[p3], sizeof(XYZ_st))==0)
  95.             continue;
  96.  
  97.         fprintf(out, "  0\n3DFACE\n  8\n%s\n  6\nCONTINUOUS\n 62\n     4\n",
  98.             (desc->name ? desc->name : "0"));
  99.  
  100.         send_XYZ(0, &desc->pnts[p1]);
  101.         send_XYZ(1, &desc->pnts[p2]);
  102.         send_XYZ(2, &desc->pnts[p3]);
  103.         send_XYZ(3, &desc->pnts[p3]);
  104.     }
  105. }
  106.  
  107. static void process_INFO(info)
  108. INFO *info;
  109. {
  110. #ifdef OLD_NFF_CODE
  111.     fprintf(out, "v\n");
  112.     if (info->obsv) {
  113.         fprintf(out, "from "); send_XYZ(&info->obsv->came);
  114.     }
  115.     if (info->ambi)
  116.         { fprintf(out, "b "); send_RGB(info->ambi); }
  117. #endif
  118. }
  119.